package com.ag.answergames.service.wallet.impl;

import com.ag.answergames.entity.AnswerCorrectEntity;
import com.ag.answergames.mapper.money.MoneyMapper;
import com.ag.answergames.mapper.question.AnswerCorrectCountMapper;
import com.ag.answergames.service.wallet.MoneyService;
import com.ag.answergames.util.SnowflakeIdWorkerController;
import com.ag.answergames.vo.common.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class MoneyServiceImpl extends ServiceImpl<MoneyMapper, AnswerCorrectEntity> implements MoneyService {

    @Autowired
    private MoneyMapper moneyMapper;
    @Autowired
    private SnowflakeIdWorkerController snowflakeIdWorkerController;


    @Override
    public Result correct(Long userId) {


        QueryWrapper<AnswerCorrectEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        AnswerCorrectEntity answerCorrectEntity = moneyMapper.selectOne(wrapper);

        if (answerCorrectEntity == null) {
            AnswerCorrectEntity answerCorrectEntity1 = new AnswerCorrectEntity();
            answerCorrectEntity1.setUserId(userId);
            answerCorrectEntity1.setCorrectCount(0);
            answerCorrectEntity1.setId(snowflakeIdWorkerController.nextId());
            moneyMapper.insert(answerCorrectEntity1);
            double v = answerCorrectEntity1.getCorrectCount();
            return Result.success(String.format("%.1f",v));
        } else {
            double v = answerCorrectEntity.getCorrectCount() * 0.1;
            return Result.success(String.format("%.1f",v));
        }

    }
}
